﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

//Importamos
using EventSys.BussinesEntities;

namespace EventSys.Repository
{
    public class DirectoresRepository:MasterRepository,IDirectoresRepository
    {
        public void RegistrarDirector(Director director)
        {
            //try
            //{
            //    var listaDirec = Context.Directors.ToList();
            //    foreach (Director d in listaDirec)
            //    {
            //        if (d.DNI == director.DNI)
            //        {
            //            throw new System.InvalidOperationException(string.Format("El DNI {0} ya existe. ", director.DNI));
            //        }
            //    }
            //    Context.Directors.Add(director);
            //    Context.SaveChanges();
            //}
            //catch (Exception)
            //{
            //    throw;
            //}  


            
            
            //var listaDirec = Context.Directors.ToList();
            //foreach (Director d in listaDirec)
            //{
            //    if (d.DNI == director.DNI)
            //    {
            //        throw new System.InvalidOperationException(string.Format("El DNI {0} ya existe. ", d.DNI));
            //    }
            //}

            Context.Directors.Add(director);
            Context.SaveChanges();
        }

        public Director DirectorById(int id)
        {
            var director = Context.Directors.Find(id);
            return director;
        }

        public void EditarDirector(Director director)
        {
            Context.Entry(director).State = System.Data.EntityState.Modified;
            Context.SaveChanges();
        }

        public void EliminarDirector(int id)
        {
            throw new NotImplementedException();
        }

        public List<Director> GetFromAllDirector()
        {
            var query = Context.Directors.ToList();
            return query;
        }

        public List<Director> GetFromAllDirectorByCriterio(string criterio)
        {
            var query = Context.Directors.ToList();
            if (criterio != "")            {
                query = (query.Where(d => d.DNI.ToUpper().Contains(criterio.ToUpper()) ||
                                          d.Nombre.ToUpper().Contains(criterio.ToUpper()) ||
                                          d.Apellidos.ToUpper().Contains(criterio.ToUpper())
                                          )
                             ).ToList();
            }
            return query;
        }


        //public bool ExisteDirectorByDNI(string dni)
        //{
        //    Boolean existe = false;
        //    //string vDni = Context.Directors.Where(d => d.DNI.Equals(director.DNI)).SingleOrDefault();

        //    //if (vDni = dni){
        //    //}

        //    var listaDirec = Context.Directors.ToList();
        //    foreach (Director d in listaDirec)
        //    {
        //        if (d.DNI == dni)
        //        {
        //            existe = true;

        //        }
        //    }
        //    return existe;
        //}
    }
}
